package logic

import (
	Article "MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"
	define2 "MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type RelatedLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRelatedLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RelatedLogic {
	return &RelatedLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RelatedLogic) Related(in *Article.ArticleRequest) (*Article.RelatedResponse, error) {
	categoryId := int(in.CategoryId)
	articleId := int(in.ArticleId)
	var articleVoList []models.ArticleVo
	models.Db.Model(new(models.ArticleVo)).
		Where("status = ?", define2.PUBLIC_ARTICLE).
		Where("category_id = ?", categoryId).
		Where("id != ?", articleId).
		Limit(define2.RELATED_ARTICLE_COUNT).
		Find(&articleVoList)
	var relatedArticleVoList []*Article.RelatedArticleVo
	copier.Copy(&relatedArticleVoList, &articleVoList)
	for i, v := range articleVoList {
		relatedArticleVoList[i].CreateTime = helper.MyTimeToTimestamp(v.CreateTime)
	}

	return &Article.RelatedResponse{
		RelatedArticleVoList: relatedArticleVoList,
	}, nil
}
